package com.itbaima.service.impl;

import com.itbaima.mapper.DepartmentMapper;
import com.itbaima.mapper.HumanMapper;
import com.itbaima.pojo.VO.HrArchiveVO;
import com.itbaima.pojo.entity.Department;
import com.itbaima.pojo.entity.HrArchive;
import com.itbaima.service.HumanService;
import com.itbaima.utils.AutoArchiveCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class HumanServiceImpl implements HumanService {

    @Autowired
    private HumanMapper humanMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(HrArchive hrArchive, Integer userId, String name) {


        hrArchive.setRegistrar(name);
        hrArchive.setRegistrarId(userId);
        hrArchive.setRegistrationTime(LocalDateTime.now());
        String archiveCode = AutoArchiveCode.generateArchiveCode(hrArchive);
        hrArchive.setArchiveCode(archiveCode);
        log.info("自动生成的档案编号如下：{}" , archiveCode);


        //   获取自身所属部门id
        //    private Integer departmentId; // 所属部门ID

        String departmentName = hrArchive.getDepartmentName();
        Integer dpId = humanMapper.getDpId(departmentName);
        hrArchive.setDepartmentId(dpId);
        humanMapper.insert(hrArchive);

    }

    @Override
    public List<HrArchiveVO> getAll() {

        List<HrArchiveVO> hrArchiveList =  humanMapper.getAll();

        return hrArchiveList;
    }

    @Override
    public void update(HrArchiveVO hrArchiveVO, String name, Integer userId) {

        // 获取资源表的id
        Integer id = hrArchiveVO.getId();

        //完善该对象其他信息
        hrArchiveVO.setRegistrar(name);
        hrArchiveVO.setRegistrar_id(userId);
        hrArchiveVO.setRegistration_time(LocalDateTime.now());
        String archiveCode = AutoArchiveCode.generateArchiveCode(hrArchiveVO);
        hrArchiveVO.setArchive_code(archiveCode);
        log.info("自动生成的档案编号如下：{}" , archiveCode);

        String departmentName = hrArchiveVO.getDepartment_name();
        Integer dpId = humanMapper.getDpId(departmentName);
        hrArchiveVO.setDepartment_id(dpId);

        log.info("查询后生成的部门id如下：{}",dpId);


        // 我现在需要重新构建其对应的上下级部门关系

        // 首先我要获取当前部门的级别

        Department department = departmentMapper.getRecentDepartment(hrArchiveVO.getDepartment_id());

        // 如果级别为1：则一二三级部门，全部改为该部门

        if(department.getLevel() == 1){

            Integer dpidnow = department.getDepartment_id();

            hrArchiveVO.setDepartment_level1_id(dpidnow);
            hrArchiveVO.setDepartment_level2_id(dpidnow);
            hrArchiveVO.setDepartment_level3_id(dpidnow);

        }
        else if(department.getLevel() == 2){

            //部门level为2的情况下

            Department dp1 = departmentMapper.getparentId(department.getParent_id());

            hrArchiveVO.setDepartment_level1_id(dp1.getDepartment_id());

            Department dp3 = departmentMapper.getSonId(department.getDepartment_id());

            hrArchiveVO.setDepartment_level3_id(dp3.getDepartment_id());

            hrArchiveVO.setDepartment_level2_id(department.getDepartment_id());
        }
        else{
            // 部门level为3的情况下

            //获取二级部门的id
            Department dp2 = departmentMapper.getparentId(department.getParent_id());
            //通过二级部门的id 获取三级部门的id
            Department dp1 = departmentMapper.getparentId(dp2.getParent_id());

            hrArchiveVO.setDepartment_level3_id(department.getDepartment_id());
            hrArchiveVO.setDepartment_level2_id(dp2.getDepartment_id());
            hrArchiveVO.setDepartment_level1_id(dp1.getDepartment_id());

        }




        humanMapper.update(hrArchiveVO);

    }


    /**
     * 删除对应id的人力资源档案
     * @param id
     */
    @Override
    public void delete(Integer id) {

    humanMapper.delete(id);


    }


}
